from email.mime.text import MIMEText
from smtplib import SMTP_SSL
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.utils import formataddr
from email import encoders


def mail_withfile(Smtp_sever, Sender, Password, Receiver, content_html, subject, file, SenderName="", ReceiverName=""):
    """ 发送带有附件的邮件 """
    message = MIMEMultipart()
    message['subject'] = subject
    message['from'] = formataddr([SenderName, Sender])
    message['to'] = formataddr([ReceiverName, Receiver])
    content = MIMEText(content_html, 'html', 'utf-8')
    message.attach(content)
    with open(file, 'rb') as f:
        attachment = MIMEBase('application', 'octet-stream')
        attachment.set_payload(f.read())
        attachment.add_header('Content-Disposition',
                              'attachment', filename=('gbk', '', f.name))
        encoders.encode_base64(attachment)
        message.attach(attachment)
    server = SMTP_SSL(Smtp_sever, 465)
    server.login(Sender, Password)
    if type(Receiver) == list:
            server.sendmail(from_addr=Sender, to_addrs=Receiver, msg=message.as_string())
    else:
        server.sendmail(from_addr=Sender, to_addrs=[Receiver, ], msg=message.as_string())
    server.quit()


def mail(Smtp_sever, Sender, Password, Receiver, content, subject, SenderName="", ReceiverName=""):
    """ 发送纯文本邮件 """
    message = MIMEText(content, 'plain', 'utf-8')
    message['From'] = formataddr([SenderName, Sender])
    message['To'] = formataddr([ReceiverName, Receiver])
    message['Subject'] = subject
    server = SMTP_SSL(Smtp_sever, 465)
    server.login(Sender, Password)
    if type(Receiver) == list:
            server.sendmail(from_addr=Sender, to_addrs=Receiver, msg=message.as_string())
    else:
        server.sendmail(from_addr=Sender, to_addrs=[Receiver, ], msg=message.as_string())
    server.quit()


def mail_html(Smtp_sever, Sender, Password, Receiver, content_html, subject, SenderName="", ReceiverName=""):
    """ 发送HTML的邮件 """
    message = MIMEMultipart()
    message['subject'] = subject
    message['from'] = formataddr([SenderName, Sender])
    message['to'] = formataddr([ReceiverName, Receiver])
    content = MIMEText(content_html, 'html', 'utf-8')
    message.attach(content)
    server = SMTP_SSL(Smtp_sever, 465)
    server.login(Sender, Password)
    if type(Receiver) == list:
            server.sendmail(from_addr=Sender, to_addrs=Receiver, msg=message.as_string())
    else:
        server.sendmail(from_addr=Sender, to_addrs=[Receiver, ], msg=message.as_string())
    server.quit()


def demo(Smtp_sever="smtp.qq.com", Sender='1006925066@qq.com', Password='*********', Receiver='1006925066@qq.com', content="""您好，这是鸭梨自动邮件服务。本邮件是由Python程序自动发送，目前还在测试阶段。
    假期时，您每天晚上8点将收到邮件和QQ信息提醒，可能加入推送内容，如果您不希望接收此类邮件可以回复信息说明，谢谢。
    我是孙振兴。
    """, subject="鸭梨信息服务", SenderName="", ReceiverName=""):
    """ 邮件Demo """
    mail_html(Smtp_sever, Sender, Password, Receiver, content, subject, "鸭梨",ReceiverName)


if __name__ == "__main__":
    demo()
